package com.yeyks.appcontroller;

import com.alibaba.fastjson.JSONObject;
import com.yeyks.config.RedisService;
import com.yeyks.constant.MessageTemplate;
import com.yeyks.dal.domain.OrderInfo;
import com.yeyks.dal.domain.UserInfo;
import com.yeyks.getui.service.GeTuiService;
import com.yeyks.model.Ajax.AjaxResponseBody;
import com.yeyks.newwebsocket.WebSocket;
import com.yeyks.rabbitmq.send.DelayQueueMsg;
import com.yeyks.rabbitmq.send.OrderQueueMsg;
import com.yeyks.service.*;
import com.yeyks.util.DateUtils;
import com.yeyks.util.Utility;
import com.yeyks.wxcode.dto.WxRefundParam;
import com.yeyks.wxcode.service.WxLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Author fuchangshuai
 * @date 2019/4/11 5:35 PM
 */
@RestController
@Slf4j
@Api(tags = "测试接口", description = "测试")
@RequestMapping(value = "/test")
public class TestController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private WebSocket webSocket;
    @Autowired
    private OrderQueueMsg orderQueueMsg;
    @Autowired
    private DelayQueueMsg delayQueueMsg;
//    @Autowired
//    private PushToSingle pushToSingle;
    @Autowired
    private SmsService smsService;
    @Autowired
    private MessageNoticeTemplateService templateService;
    @Autowired
    private MessageNoticeService noticeService;
    @Autowired
    private RechargeRecordService recordService;
    @Autowired
    private PayService payService;
    @Autowired
    private GeTuiService geTuiService;
    @Autowired
    private WxLoginService wxLoginService;

    /**
     * 测试redis工具
     *
     * @param token
     * @return
     */
    @ApiIgnore
    @ApiOperation(value = "测试redis工具", notes = "测试redis工具")
    @ApiImplicitParam(paramType = "query", name = "token", value = "token", required = true, dataType = "String")
    @PostMapping(value = "/redis")
    public ResponseEntity<?> testRedis(@RequestParam(name = "token") String token) {
        UserInfo userInfo = userInfoService.tokenUserInfo(token);
        redisService.setValueTimeOut(String.valueOf(userInfo.getId()), token, 1000 * 60 * 60 * 24 * 10);
        return ResponseEntity.ok(AjaxResponseBody.success(redisService.getExpire(String.valueOf(userInfo.getId()))));
    }

    /**
     * 测试发消息
     *
     * @return
     */
    @ApiIgnore
    @ApiOperation(value = "测试发消息", notes = "测试发消息")
    @PostMapping(value = "/webSocket")
    public ResponseEntity<?> testWebSocket() {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderNo("123456785678ertyu");
        orderInfo.setSnapshotHotelName("万达酒店");
        orderInfo.setSnapshotRoomName("大床房");
        List<Integer> list = new ArrayList<>();
        list.add(1679);
        list.add(1403);
        list.add(1696);
        for (Integer ii : list) {
            webSocket.sendMessageToMore(orderInfo.getOrderNo(), String.valueOf(ii));
        }
//        webSocket.sendMessageTo(orderInfo, "测试");
        return ResponseEntity.ok(AjaxResponseBody.success());
    }

    /**
     * 测试rabbit发消息
     *
     * @return
     */
    @ApiIgnore
    @ApiOperation(value = "测试rabbit发消息", notes = "测试rabbit发消息")
    @PostMapping(value = "/rabbit/webSocket")
    public ResponseEntity<?> testRabbitWebSocket() {
        Map<String, String> map = new HashMap<>();
        map.put("message", "12345678");
        map.put("userId", "APNPayload");
//        orderQueueMsg.send(JSONObject.toJSONString(map));
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderNo("123456789");
        orderInfo.setUserId(432);
        orderQueueMsg.sendToB(orderInfo);
        return ResponseEntity.ok(AjaxResponseBody.success());
    }

    /**
     * 测试个推
     *
     * @return
     */
    @ApiIgnore
    @ApiOperation(value = "测试个推", notes = "测试个推")
    @PostMapping(value = "/getui")
    public ResponseEntity<?> testGetui() {
        Map<String, String> map = new HashMap<>();
        map.put("message", "12345678");
        map.put("userId", "122");
        String content = JSONObject.toJSONString(map);
        geTuiService.singlePush("122", "测试发送", content);
        return ResponseEntity.ok(AjaxResponseBody.success());
    }

    /**
     * 测试短信
     *
     * @return
     */
    @ApiIgnore
    @ApiOperation(value = "测试短信", notes = "测试短信")
    @PostMapping(value = "/sendSms")
    public ResponseEntity<?> testSendSms() throws Exception {
        String phone = "15188445491";
        String person = "付常帅";
        String daytime = "2019-03-28";
        String hotelname = "如家酒店";
        String roomtype = "大床房";
        String roomnum = "2";
        String roomnight = "3";
        String address = "杭州市.西湖区.天瑞国际";
        String hoteltel = "15188445491";
        //给用户发送下单成功提示短信(模版id有问题)
//        return ResponseEntity.ok(AjaxResponseBody.success(smsService.sendSmsToUserForOrderComplete(phone, person, daytime, hotelname, roomtype, roomnum, roomnight, address, hoteltel)));
        //用户下单成功酒店通知短信
//        return ResponseEntity.ok(AjaxResponseBody.success(smsService.sendSmsToHotelForOrderComplete(phone, person, daytime, hotelname, roomtype, roomnum, roomnight)));
        // 给用户退款成功后发送短信通知酒店
//        return ResponseEntity.ok(AjaxResponseBody.success(smsService.sendSmsForUserRefund(phone, person, daytime, hotelname, roomtype)));
        //当酒店无法接单时退款，给用户发送短信通知
        return ResponseEntity.ok(AjaxResponseBody.success(smsService.sendSmsForHotelRefund(phone, daytime, hotelname, roomtype)));
    }

    /**
     * 测试模板
     *
     * @return
     */
    @ApiIgnore
    @ApiOperation(value = "测试模板", notes = "测试模板")
    @PostMapping(value = "/template")
    public ResponseEntity<?> testTemplate() {
        String hotelName = "拱墅区测试酒店";
        String checkIn = "2019-05-03";
        String checkOut = "2019-05-04";
        Integer count = 2;
        Map<String, String> map = templateService.orderAfterHandleValue(MessageTemplate.ORDER_USER_REFUND, hotelName, checkIn, checkOut, count);

//        Map<String, Object> params = new HashMap<>();
//        params.put("phone", "15188445491");
//        params.put("amount", 10);
//        params.put("count", 2);
//        Map<String, String> map = templateService.commonValue(params, MessageTemplate.INVATE_COUPON);
        return ResponseEntity.ok(AjaxResponseBody.success(map));
    }

    /**
     * 测试插入消息数据
     *
     * @return
     */
    @ApiIgnore
    @ApiOperation(value = "测试插入消息数据", notes = "测试插入消息数据")
    @PostMapping(value = "/template/notice")
    public ResponseEntity<?> testTemplateNotice() {
        String hotelName = "拱墅区测试酒店";
        String checkIn = "2019-05-03";
        String checkOut = "2019-05-04";
        Integer count = 2;
        Map<String, String> map = templateService.orderAfterHandleValue(MessageTemplate.ORDER_ACCEPT, hotelName, checkIn, checkOut, count);
        noticeService.createNoticeAndSave(map, 111, 354);
        return ResponseEntity.ok(AjaxResponseBody.success(map));
    }

    /**
     * 根据用户id获取用户token
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "根据用户id获取用户token", notes = "根据用户id获取用户token")
    @ApiImplicitParam(paramType = "query", name = "userId", value = "用户id", required = true, dataType = "int")
    @PostMapping(value = "/getToken")
    public ResponseEntity<?> getToken(@RequestParam(name = "userId") Integer userId) {
        return ResponseEntity.ok(AjaxResponseBody.success(userInfoService.getTokenByUseId(userId)));
    }

    /**
     * 给用户生成邀请码
     *
     * @return
     */
    @ApiIgnore
    @ApiOperation(value = "给用户生成邀请码", notes = "给用户生成邀请码")
    @PostMapping(value = "/create/code")
    public ResponseEntity<?> createCode() {
        userInfoService.createCode();
        return ResponseEntity.ok(AjaxResponseBody.success());
    }

    /**
     * 微信退款测试
     *
     * @return
     */
    @ApiIgnore
    @ApiOperation(value = "微信退款测试", notes = "微信退款测试")
    @PostMapping(value = "/wx/refund")
    public ResponseEntity<?> wxRefund() {
        WxRefundParam wxpay = new WxRefundParam("wxpay", "1557127731866al7b07g", "1557127731866al7b07g888", 1, 1);
        return ResponseEntity.ok(AjaxResponseBody.success(wxLoginService.disPoseRefund(wxpay)));
    }

    /**
     * 用户提现
     *
     * @return
     */
    @ApiIgnore
    @ApiOperation(value = "用户提现", notes = "用户提现")
    @PostMapping(value = "/extract")
    public ResponseEntity<?> extract(HttpServletRequest httpServletRequest) {
        String ip = Utility.getRemoteIP(httpServletRequest);

        payService.wxTransV2("oCAEs04a91kEoIqbZvp8aV0EEpJU", 1, "127.0.0.1");
        return ResponseEntity.ok(AjaxResponseBody.success());
    }
}
